﻿/// API HELPERS

function getSelectedItem(selectE) {
    var index = selectE.selectedIndex;
    if (index < 0) {
        return;
    }
    return selectE.options[index].value;
}

// get the json wqe description 
function getQuery(short_desc) {
    return getItem(_jsonQueries, "short_desc", short_desc);
}

function getTable(tablename) {
    return getItem(_jsonTables, "tablename", tablename);
}

function getItem(store, attr, key) {
    if (!store) return null;
    for (var x in store) {
        if (store[x][attr] == key) {
            return store[x];
        }
    }
    return null;
}

///////////////////////
//// UTILITY FUNCTIONS
/////////////////////

// get the document.getElementById shortname
function $id(id) {
    var e = document.getElementById(id);
    if (!e) { // sometimes the script manager renames our runat=server msgs, so we prepend 'ctl00_main_' 
        e = document.getElementById("ctl00_main_" + id);
    }
    return e;
}

// turn json text into a json object
function toJSON(text) { return (new Function("return " + text))() }

String.prototype.trim = function () { return this.replace(/^\screen+|\screen+$/, '') }

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function () {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}